package com.hzvdvmc.autosale.setting.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzvdvmc.autosale.MyApp;
import com.hzvdvmc.autosale.R;
import com.hzvdvmc.autosale.base.ActionbarActivity;
import com.hzvdvmc.autosale.base.BaseActivity;
import com.hzvdvmc.autosale.constant.SpKey;
import com.hzvdvmc.autosale.setting.bean.RoleEnum;
import com.hzvdvmc.autosale.setting.bean.Users;
import com.hzvdvmc.autosale.util.GsonUtils;
import com.hzvdvmc.autosale.util.LogUtil;
import com.hzvdvmc.autosale.util.Md5Util;
import com.hzvdvmc.autosale.util.NetworkUtils;
import com.hzvdvmc.autosale.util.SPUtils;
import com.hzvdvmc.autosale.util.ToastUtils;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 登录页面
 */
public class LoginActivity extends ActionbarActivity {

    @BindView(R.id.edt_psw)
    EditText edtPsw;
    @BindView(R.id.btn_manager)
    Button btnManager;
    @BindView(R.id.edt_account)
    EditText edtAccount;
    @BindView(R.id.tv_tips)
    TextView tvTips;

    //管理员登录
    @OnClick(R.id.btn_manager)
    public void loginByManager() {

        String account = getAccount();
        String psw1 = getPsw();
        if (TextUtils.isEmpty(account)) {
            ToastUtils.showShort("请输入账号");
            return;
        }
        if (TextUtils.isEmpty(psw1)) {
            ToastUtils.showShort("请输入密码");
            return;
        }

        String json = SPUtils.getInstance().getString(SpKey.USER_INFO);
        Users users = GsonUtils.GsonToBean(json, Users.class);
        if (users != null) {
            HashMap<String, String> map = users.getMap();
            boolean isContain = map.keySet().contains(account);
            if (isContain) {
                String psw = map.get(account);
                if (psw.equals(Md5Util.encode(psw1))) {
                    SelectFunctionActivity.startMe(this, getRoleByName(account), account);
                    LogUtil.p(account + "，登录了");
                    finish();
                } else {
                    ToastUtils.showShort("密码错误");
                    edtPsw.getText().clear();
                }
            } else {
                ToastUtils.showShort("账号不存在");
            }
        }
    }

    /**
     * 5s后隐藏
     */
    private void hideAfterTime() {
        Timer timer = new Timer();
        final TimerTask task = new TimerTask() {
            @Override
            public void run() {
                // 需要做的事:发送消息
                tvTips.setVisibility(View.GONE);
            }
        };
        timer.schedule(task, 5000);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        setCustomeActiorBar("登录");

        initAccount();

        try {
            MyApp.stopSerialHeart();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    private void initAccount() {
        String json = SPUtils.getInstance().getString(SpKey.USER_INFO);
        Users users = GsonUtils.GsonToBean(json, Users.class);
        if (users == null) {
            HashMap<String, String> map = new HashMap<>();
            map.put("superadmin", Md5Util.encode("888888"));
            map.put("ft001", Md5Util.encode("123456"));
            map.put("ft002", Md5Util.encode("123456"));
            map.put("ft003", Md5Util.encode("123456"));
            map.put("ft004", Md5Util.encode("123456"));
            map.put("ft005", Md5Util.encode("123456"));
            map.put("ft006", Md5Util.encode("123456"));
            map.put("ft007", Md5Util.encode("123456"));
            map.put("ft008", Md5Util.encode("123456"));
            map.put("ft009", Md5Util.encode("123456"));
            map.put("ft010", Md5Util.encode("123456"));
            map.put("ft011", Md5Util.encode("123456"));
            map.put("ft012", Md5Util.encode("123456"));
            map.put("ft013", Md5Util.encode("123456"));
            map.put("ft014", Md5Util.encode("123456"));
            map.put("ft015", Md5Util.encode("123456"));
            map.put("ft016", Md5Util.encode("123456"));
            map.put("admin", Md5Util.encode("111111"));
            map.put("suppler", Md5Util.encode("123456"));
            users = new Users(map);
            String userInfo = GsonUtils.GsonString(users);
            SPUtils.getInstance().put(SpKey.USER_INFO, userInfo);
        }

    }

    public String getAccount() {
        return edtAccount.getText().toString();
    }

    public String getPsw() {
        return edtPsw.getText().toString();
    }

    private RoleEnum getRoleByName(String name) {
        if (TextUtils.isEmpty(name)) {
            return null;
        }
        if (name.equals("admin")) {
            return RoleEnum.Admin;
        }
        if (name.equals("superadmin")) {
            return RoleEnum.Superadmin;
        }
        if (name.equals("suppler")) {
            return RoleEnum.Suppler;
        }
        if (name.startsWith("ft")) {
            return RoleEnum.Factory;
        }
        return null;
    }

}
